import dayjs from "dayjs";
import proj4 from "proj4";

// 反编译html
export const htmlDecode = (text: string) => {
	const reg = /<[^>]+>/g;
	if (reg.test(text)) {
		return text;
	}
	let temp = document.createElement("div");
	temp.innerHTML = text;
	const output = temp.innerText || temp.textContent;
	temp = null;
	return output;
};

// 判断是否为空
export function isEmpty(o) {
	return typeof o === "undefined" || o === "" || o === null ? "-" : o;
}

export function getBase64(img: Blob, callback: (base64Url: string) => void) {
	const reader = new FileReader();
	reader.addEventListener("load", () => callback(reader.result as string));
	reader.readAsDataURL(img);
}

/**
 *
 * @param date 可被dayjs接受的时间格式
 * @param type 时间格式如"YYYY年MM月DD日 HH时mm分";
 */
export function timeFormatter(date: any, type: string) {
	if (!date) {
		return "-";
	}
	try {
		return dayjs(date).format(type);
	} catch (exx) {
		console.error(exx);
		return dayjs();
	}
}

// 基础类型 数组去重
export function noRepeatArr(arr) {
	// Set结构不能接收重复数据的特点
	const newArr = [...new Set(arr)];
	return newArr;
}


// 判断是否为原生html
export const isRawHtml = (html) => {
	return (
		/^<[a-z]+[\s\S]*><[a-z]+[\s\S]*>/.test(html) ||
		/^<[a-z]+[\s\W]style=/.test(html)
	);
};

// 富文本内容通用转换-手动处理富文本表格样式问题
export const convertHtmlDocs = (noticeContent: string) => {
	let html = noticeContent;

	if (!isRawHtml(html)) {
		html = htmlDecode(html);
	}
	// 对table标签做处理，table标签外侧再套一层div
	const reg = /<table[^>]+>/g;
	if (reg.test(html)) {
		html = html.replace(
			"<table",
			"<div style='width:100%; overflow: auto;padding: 4px'><table"
		);
		html = html.replace("</table>", "</table></div>");
	}
	// 对img标签做处理
	const imgReg = /<img[^>]+>/g;
	if (imgReg.test(html)) {
		html = html.replace(
			"<img",
			"<div style='width:100%; overflow: auto;padding: 4px'><img"
		);
		html = html.replace("</img>", "</img></div>");
	}

	// 处理缩进负号的text-indent
	html = html.replace(
		/text\-indent\: -(\s*\d+(?:\.\d+)?)(?:pt|PT|px|PX)\s*(\;*)/gi,
		function (mathch, key, key1) {
			return "text-indent: 0;";
		}
	);

	html = html.replace(
		/TEXT\-INDENT\: -(\s*\d+(?:\.\d+)?)(?:pt|PT|px|PX)\s*(\;*)/gi,
		function (mathch, key, key1) {
			return "text-indent: 0;";
		}
	);

	return html;
};


// 处理富文本字号
export const convertHtmlFontsize = (noticeContent: string, isAdd: boolean) => {
	let html = noticeContent;
	// font-size:px
	html = html.replace(
		/font\-size\:(\s*\d+(?:\.\d+)?)px\s*(\;*)/gi,
		function (mathch, key, key1) {
			const fontSize = isAdd ? parseInt(key) + 2 : parseInt(key) - 2;
			return "font-size:" + `${fontSize}` + "px" + key1;
		}
	);
	// font-size:pt
	html = html.replace(
		/font\-size\:(\s*\d+(?:\.\d+)?)pt\s*(\;*)/gi,
		function (mathch, key, key1) {
			const fontSize = isAdd ? parseInt(key) + 1 : parseInt(key) - 1;
			return "font-size:" + `${fontSize}` + "pt" + key1;
		}
	);
	// line-height:px
	html = html.replace(
		/line\-height\:(\s*\d+(?:\.\d+)?)px\s*(\;*)/gi,
		function (mathch, key, key1) {
			return "line-height: 1.5;";
		}
	);
	// LINE-HEIGHT:px
	html = html.replace(
		/LINE\-HEIGHT\:(\s*\d+(?:\.\d+)?)px\s*(\;*)/gi,
		function (mathch, key, key1) {
			return "line-height: 1.5;";
		}
	);
	// line-height:px
	html = html.replace(
		/line\-height\:(\s*\d+(?:\.\d+)?)pt\s*(\;*)/gi,
		function (mathch, key, key1) {
			return "line-height: 1.5;";
		}
	);
	// LINE-HEIGHT:px
	html = html.replace(
		/LINE\-HEIGHT\:(\s*\d+(?:\.\d+)?)pt\s*(\;*)/gi,
		function (mathch, key, key1) {
			return "line-height: 1.5;";
		}
	);
	return html;
};

// 图片url转base64
export const image2Base64 = (url: string) => new Promise((resolve, reject) => {
  if (!url) {
    resolve("");
    return;
  }
  const img = new Image();
  img.crossOrigin = "anonymous";
  img.src = url;
  img.onload = () => {
    const canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    const ctx = canvas.getContext("2d");
    ctx?.drawImage(img, 0, 0);
    const data = canvas.toDataURL();
    resolve(data);
  };
  img.onerror = () => {
    reject("");
  };
});
